package com.yanfei.zero.domain.order.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.media.Schema.AccessMode;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.Id;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import jakarta.persistence.Transient;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Positive;
import lombok.Data;  
@Entity
@Table(name="yf_order")
@Data  
@Schema(description = "订单对象")  
public class OrderPO implements Serializable {  
    public OrderPO() {  
    } 
	@Id
    @NotBlank(message = "订单号不能为空")  
    @Schema(name = "订单号", example = "ORD123456")  
    private String orderId;  
  
    @NotNull(message = "用户ID不能为空")  
    @Schema(name = "用户ID", example = "1001")  
    private String userId;  
  
    @NotNull(message = "商品数量不能为空")  
    @Positive(message = "商品数量必须为正数")  
    @Schema(name = "商品数量", example = "2")  
    private Integer quantity;  
  
    @NotNull(message = "订单总价不能为空")  
    @Positive(message = "订单总价必须为正数")  
    @Schema(name = "订单总价", example = "1999.00")  
    private Double totalPrice=0.0;  
  
    @Schema(name = "创建时间", example = "2023-10-27T10:00:00Z", accessMode = AccessMode.READ_ONLY)  
    private Date createTime;  
  
    @Schema(name = "最后修改时间", example = "2023-10-27T10:05:00Z", accessMode = AccessMode.READ_ONLY)  
    private Date lastModifiedTime;  
  
    @Schema(name = "支付状态", example = "PENDING")  
    private String paymentStatus;  
    
    
    @Schema(name = "支付时间", example = "2023-10-28T10:00:00Z")  
    private Date paymentTime;  
  
    @Schema(name = "配送状态", example = "NOT_SHIPPED")  
    private String shippingStatus;  
    
    @Schema(name = "配送时间", example = "2023-10-28T10:00:00Z")  
    private Date shippingTime;  
  
    @Schema(name = "配送单号", example = "EX123456789CN")  
    private String shippingTrackingNumber;  
    
    @Schema(name = "是否确认收货", example = "true")  
    private Boolean isConfirmed;  
  
    @Schema(name = "自动确认收货时间", example = "2023-10-28T10:00:00Z")  
    private Date autoConfirmTime;  
  
    @Schema(name = "用户评价", example = "商品质量很好，配送也很快！")  
    private String userReview;  
  
    @Schema(name = "评价星级(1-5)", example = "5")  
    private Integer reviewRating;  
    
    @Schema(name = "是否取消订单", example = "false")  
    private Boolean isCancelled;  
  
    @Schema(name = "取消订单时间", example = "2023-10-27T12:00:00Z")  
    private Date cancellationTime;  
  
    @Schema(name = "退款状态", example = "PENDING")  
    private String refundStatus;  
  
    @Schema(name = "退款金额", example = "199.00")  
    private Double refundAmount;  
  
    @Schema(name = "退款处理时间", example = "2023-10-28T10:00:00Z")  
    private Date refundTime; 
    @Transient
    @OneToMany(cascade=CascadeType.ALL,fetch=FetchType.LAZY,mappedBy = "orderPO")
    private List<OrderItem> orderItems = new ArrayList<>();  
    
    
    // 构造方法  
    public OrderPO(String orderId, String userId) {  
        this.orderId = orderId;  
        this.userId = userId;  
        this.createTime = new Date(); // 订单创建时间设为当前时间  
        this.isCancelled = false; // 默认情况下订单未被取消  
    }  
    
    // 添加商品到订单  
    public void addItem(OrderItem item) {  
    	this.totalPrice = this.totalPrice+item.getPrice()*item.getQuantity();
    	this.quantity = item.getQuantity()+item.getQuantity();
        this.orderItems.add(item);  
    }  
  
    // 订单支付  
    public void pay(String status) {  
        this.paymentStatus = status;  
        this.paymentTime = new Date(); // 支付时间设为当前时间  
    }  
  
    // 订单发货  
    public void ship(String status) {  
        this.shippingStatus = status;  
        this.shippingTime = new Date(); // 发货时间设为当前时间  
    }  
    
    //判断是否应该自动确认收货  
    public boolean shouldAutoConfirm() {  
        if (autoConfirmTime == null) {  
            return false;  
        }  
        Date now = new Date();  
        return now.after(autoConfirmTime);  
    }  
    
    // 设置自动确认收货的时间  
    public void setAutoConfirmTime(int daysAfterShipment) {  
        if (shippingStatus != null && shippingStatus.equals("SHIPPED") && createTime != null) {  
            Calendar calendar = Calendar.getInstance();  
            calendar.setTime(createTime);  
            calendar.add(Calendar.DAY_OF_MONTH, daysAfterShipment);  
            this.autoConfirmTime = calendar.getTime();  
        }  
    }  
    // 添加取消订单的方法  
    public void cancelOrder() {  
        this.isCancelled = true;  
        this.cancellationTime = new Date();  
        // 可能还需要更新其他字段或发送通知  
    }  
  
    // 添加申请退款的方法  
    public void applyRefund(Double amount) {  
        if (amount > 0 && amount <= this.totalPrice) {  
            this.refundStatus = "PENDING";  
            this.refundAmount = amount;  
            // 发送退款申请到支付系统或相关服务  
        }  
    }  
  
    // 添加处理退款的方法  
    public void processRefund() {  
        if (this.refundStatus.equals("PENDING")) {  
            // 假设退款成功，更新退款状态和处理时间  
            this.refundStatus = "SUCCEEDED";  
            this.refundTime = new Date();  
            // 可能还需要更新订单状态或发送通知  
        }  
    }  
  
    // 检查订单是否可以被取消  
    public boolean canCancel() {  
        // 根据业务逻辑决定订单是否可以被取消，例如：订单未支付、未发货等  
        return !this.isCancelled && !this.paymentStatus.equals("COMPLETED");  
    }  
  
    // 检查订单是否可以申请退款  
    public boolean canApplyRefund() {  
        // 根据业务逻辑决定订单是否可以申请退款，例如：订单已支付但未确认收货  
        return !this.isCancelled && this.paymentStatus.equals("COMPLETED") && !this.isConfirmed;  
    }
    
 
}